import { action, observable } from 'mobx';
import { message } from 'antd';
import taskServer from '../services/task';
import projectServer from '../services/project';
import isSuccess from '../utils/error';
import Base from './base';

const {
    getList, detail, complete, restart, createTask,
    saveinfo, repeatType, aheadType, saveRemind,
    addMember, deleteMember, setManager, setHost,
    alarmTaskList
} = taskServer;

const { linkProjectList } = projectServer;

const membersApi = {
    addMember, deleteMember, setManager, setHost
}

export default class TaskState extends Base {

    constructor(props) {
        super(props);
        // 注入其它需要调用的store
        setTimeout(() => {
            const { contactsState } = props;
            this.contactsState = contactsState;
        })

    }

    @observable taskList = [];
    @observable taskList0 = []; // 未完成任务列表
    @observable taskList1 = []; // 已完成任务列表
    @observable detailData = undefined;
    @observable detailDataForSide = undefined // 侧边栏显示的detalData，避免于detailData冲突

    @observable linkProjectList = [];
    @observable alarmTaskList = [];
    @observable repeatList = [];
    @observable aheadList = [];
    @observable loaded = {
        project: false,
        task: false,
        detailSelect: false,
    };

    @action.bound
    async getList(params, callback) {
        const res = await getList(params);
        if (isSuccess(res)) {
            const list = res.data && Array.isArray(res.data) ? res.data : [];
            callback && callback(list);
            this.taskList = list;
            if (list.length) {
                const taskList0 = [];
                const taskList1 = [];
                list.forEach(l => {
                    l.taskstatus === 2 ? taskList1.push(l) : taskList0.push(l);
                });
                this.taskList0 = taskList0;
                // 组内排序由完成时间大到小排序
                let sortTaskList1 = taskList1;
                if (taskList1.length && taskList1.length > 1) {
                    sortTaskList1 = taskList1.concat().sort((a, b) => {
                        return Date.parse(new Date(b.completedtime)) - Date.parse(new Date(a.completedtime));
                    });
                }
                this.taskList1 = sortTaskList1;
            }

        }
    }

    @action.bound
    async getDetailSelectList() {
        this.loaded.detailSelect = false;
        // 获取该任务重复频度类型
        const repeatList = await repeatType();
        this.repeatList = repeatList.data;
        // 获取该任务提前时间类型
        const aheadList = await aheadType();
        this.aheadList = aheadList.data;
        this.loaded.detailSelect = true;
    }

    @action.bound
    async getLinkProjectList(searchkey = '') {
        // 获取该任务可绑定的项目列表
        this.loaded.project = false;
        const res = await linkProjectList({ searchkey });
        if (isSuccess(res)) {
            // console.log('---getLinkProjectList---', res);
            this.linkProjectList = res.data;
        }
        this.loaded.project = true;
    }

    @action.bound
    async getAlarmTaskList() {
        this.loaded.task = false;
        // 获取任务提醒列表
        const res = await alarmTaskList();
        if (isSuccess(res)) {
            // console.log('---getAlarmTaskList---', res);
            this.alarmTaskList = res.data;
        }
        this.loaded.task = true;
    }

    @action
    async getDetail({ taskid }, reflesh) {
        const res = await detail({ taskid });
        if (isSuccess(res)) {
            this.detailData = res.data;
        }
    }

    @action
    async getDetailForSide({ taskid }) {
        const res = await detail({ taskid });
        if (isSuccess(res)) {
            this.detailDataForSide = res.data;
        }
    }

    @action.bound
    async complete(params, callback) {
        const res = await complete(params);
        if (isSuccess(res)) {
            callback && callback();
        }
    }

    @action.bound
    async restart(params, callback) {
        const res = await restart(params);
        if (isSuccess(res)) {
            callback && callback();
        }
    }

    @action.bound
    async createTask(params, callback) {
        const res = await createTask(params);
        if (isSuccess(res)) {
            // console.log('---createTask---', res);
            message[['error', 'success'][res.success]](res.message);
            callback && callback();
        }
    }

    @action.bound
    async saveinfo(params) {
        const res = await saveinfo(params);
        if (isSuccess(res)) {
            message[['error', 'success'][res.success]](res.message);
            if (!res.success) return;
            // 保持成功刷新列表和日期提醒数据
            this.getAlarmTaskList()

        }
    }

    @action
    async saveRemind(params, callback) {
        const res = await saveRemind(params);
        if (isSuccess(res)) {
            message[['error', 'success'][res.success]](res.message);
            console.log('---saveRemind---', res);
            callback && callback();
        }
    }

    // 任务成员请求接口的通用处理
    @action.bound
    async manageMember(key, params, callback) {
        const res = await membersApi[key](params);
        if (isSuccess(res)) {
            callback && callback();
            message[['error', 'success'][res.success]](res.message);
        }
    }
}
